home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / mtl100je.zip / EXAMPLE2.CPP < prev    next >
C/C++ Source or Header  |  1993-05-06  |  2KB  |  69 lines

  1. //--------------------------------------------------------------------------
  2. //
  3. //      EXAMPLE2.CPP: example for DOS multithreading library.
  4. //      Copyright (c) J.English 1993.
  5. //      Author's address: je@unix.brighton.ac.uk
  6. //
  7. //      Permission is granted to use copy and distribute the
  8. //      information contained in this file provided that this
  9. //      copyright notice is retained intact and that any software
  10. //      or other document incorporating this file or parts thereof
  11. //      makes the source code for the library of which this file
  12. //      is a part freely available.
  13. //
  14. //--------------------------------------------------------------------------
  15. //
  16. //      This example starts 3 threads, each of which repeatedly displays
  17. //      a message and then delays for either 3, 5 or 7 seconds.
  18. //
  19. //--------------------------------------------------------------------------
  20.  
  21. #include <stdio.h>
  22. #include <conio.h>
  23. #include "threads.h"
  24.  
  25. class Example2 : public DOSThread
  26. {
  27.   public:
  28.     Example2 (int n, int s)      { num = n; secs = s; }
  29.  
  30.   protected:
  31.     virtual void main ();
  32.  
  33.   private:
  34.     int num;
  35.     int secs;
  36. };
  37.  
  38. void Example2::main ()
  39. {
  40.     char c [70];
  41.     sprintf (c, "Starting thread %d, %d second delays\n", num, secs);
  42.     fputs (c, stdout);
  43.     sprintf (c, "Thread %d\n", num);
  44.     while (!userbreak ())
  45.     {   fputs (c, stdout);
  46.         delay (secs * 18);      // 1 second = 18 clock ticks (approx.)
  47.     }
  48.     sprintf (c, "End of thread %d\n", num);
  49.     fputs (c, stdout);
  50. }
  51.  
  52. void main ()
  53. {
  54.     Example2 e1 (1, 3);
  55.     Example2 e2 (2, 5);
  56.     Example2 e3 (3, 7);
  57.  
  58.     puts ("Press CONTROL-BREAK to terminate");
  59.     puts ("Press any key to start...");
  60.     getch ();
  61.  
  62.     if (!e1.run ())
  63.         puts ("Couldn't start thread 1");
  64.     if (!e2.run ())
  65.         puts ("Couldn't start thread 2");
  66.     if (!e3.run ())
  67.         puts ("Couldn't start thread 3");
  68. }
  69.